#include "LedControl.h"
#include <QPainter>

LedControl::LedControl(QWidget *parent,LED_COLOUR colour) :
    QWidget(parent),ledcolour(colour)
{
    switch(ledcolour)
    {
    case RED:ledpixmap=QPixmap("/qt/sterilization/images/redled.png","png");break;
    case BLUE:ledpixmap=QPixmap("/qt/sterilization/images/blueled.png","png");break;
    case PURPLE:ledpixmap=QPixmap("/qt/sterilization/images/purpleled.png","png");break;
    case GREEN:ledpixmap=QPixmap("/qt/sterilization/images/greenled.png","png");break;
    case YELLOW:ledpixmap=QPixmap("/qt/sterilization/images/yellowled.png","png");break;
    default:break;
    }

    grayled=QPixmap("/qt/sterilization/images/grayled.png","png");
    timer=NULL;
    w=h=10;
}

LedControl::~LedControl()
{
    delete timer;
}
/****/
void LedControl::paintEvent(QPaintEvent *)
{

    QPainter painter(this);
    if(b)
    {
        painter.drawPixmap(0,0,w,h,ledpixmap);
    }
    else
    {
        painter.drawPixmap(0,0,w,h,grayled);
    }


}/*******/

void LedControl::resizeEvent(QResizeEvent *)
{
    h=height();
    w=width();
    if( h> w ) h=w;
    else w=h;
    resize (w,h);
}


void LedControl::time_done()
{
    if(b) b=false;
    else b=true;
    update();
}

void LedControl::flash(uint ms)
{
    if(timer==NULL)
    {
    timer=new QTimer();
    QObject:: connect( timer, SIGNAL(timeout()), SLOT(time_done()) );
    }
    timer->start(ms);

}
void LedControl::stop_flash(bool onoroff)
{
   timer->stop();
   power(onoroff);

}
void LedControl::power(bool onoroff)
{
    b=onoroff;
    update();
}

QString LedControl::name()
{
 return namestr;

}
void LedControl::setname(QString &str)
{
    namestr=str;
}
